//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.timeline.item
//
//import com.intellij.collaboration.async.mapState
//import com.intellij.collaboration.ui.codereview.comment.CodeReviewSubmittableTextViewModelBase
//import com.intellij.collaboration.ui.codereview.comment.CodeReviewTextEditingViewModel
//import com.intellij.collaboration.ui.icon.IconsProvider
//import com.intellij.collaboration.util.SingleCoroutineLauncher
//import com.intellij.openapi.progress.EmptyProgressIndicator
//import com.intellij.openapi.project.Project
//import com.intellij.openapi.util.NlsSafe
//import com.intellij.platform.util.coroutines.childScope
//import com.intellij.util.io.await
//import kotlinx.coroutines.CoroutineName
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.cancel
//import kotlinx.coroutines.flow.*
//import com.linqingying.gitcode.api.data.*
//import com.linqingying.gitcode.pullrequest.comment.convertToHtml
//import com.linqingying.gitcode.pullrequest.data.GCPRDataContext
//import com.linqingying.gitcode.pullrequest.data.provider.GCPRCommentsDataProvider
//import com.linqingying.gitcode.pullrequest.ui.emoji.GCReactionViewModelImpl
//import com.linqingying.gitcode.pullrequest.ui.emoji.GCReactionsViewModel
//import java.util.*
//
//interface GCPRTimelineCommentViewModel {
//  val author: GCActor
//  val createdAt: Date
//
//  val bodyHtml: StateFlow<@NlsSafe String>
//
//  val isBusy: StateFlow<Boolean>
//
//  val canDelete: Boolean
//
//  val canEdit: Boolean
//  val editVm: StateFlow<CodeReviewTextEditingViewModel?>
//
//  val canReact: Boolean
//  val reactionsVm: GCReactionsViewModel
//
//  fun editBody()
//
//  fun delete()
//}
//
//internal class UpdateableGCPRTimelineCommentViewModel(
//  private val project: Project,
//  parentCs: CoroutineScope,
//  dataContext: GCPRDataContext,
//  private val commentsData: GCPRCommentsDataProvider,
//  initialData: GCIssueComment
//) : GCPRTimelineCommentViewModel, GCPRTimelineItem.Comment {
//  private val cs = parentCs.childScope(CoroutineName("GitCode Pull Request Timeline Comment View Model"))
//  private val taskLauncher = SingleCoroutineLauncher(cs)
//
//  private val currentUser: GCUser = dataContext.securityService.currentUser
//  private val ghostUser: GCUser = dataContext.securityService.ghostUser
//  private val reactionIconsProvider: IconsProvider<GCReactionContent> = dataContext.reactionIconsProvider
//
//  private val dataState = MutableStateFlow(initialData)
//
//  private val id = initialData.id
//  override val author: GCActor = initialData.author ?: ghostUser
//  override val createdAt: Date = initialData.createdAt
//
//  override val bodyHtml: StateFlow<String> = dataState.map {
//    it.body.convertToHtml(project)
//  }.stateIn(cs, SharingStarted.Eagerly, "")
//
//  override val isBusy: StateFlow<Boolean> = taskLauncher.busy
//
//  override val canDelete: Boolean = initialData.viewerCanDelete
//  override val canEdit: Boolean = initialData.viewerCanUpdate
//  override val canReact: Boolean = initialData.viewerCanReact
//
//  private val _editVm = MutableStateFlow<EditViewModel?>(null)
//  override val editVm: StateFlow<CodeReviewTextEditingViewModel?> = _editVm.asStateFlow()
//
//  private val reactions: StateFlow<List<GCReaction>> = dataState.mapState { it.reactions.nodes }
//  override val reactionsVm: GCReactionsViewModel = GCReactionViewModelImpl(
//    cs, id, reactions, currentUser, dataContext.reactionsService, reactionIconsProvider
//  )
//
//  override fun editBody() {
//    val currentText = dataState.value.body
//    _editVm.update {
//      (it ?: EditViewModel(currentText)).apply {
//        requestFocus()
//      }
//    }
//  }
//
//  private fun stopEditing() {
//    _editVm.update {
//      it?.dispose()
//      null
//    }
//  }
//
//  override fun delete() {
//    taskLauncher.launch {
//      commentsData.deleteComment(EmptyProgressIndicator(), dataState.value.id)
//    }
//  }
//
//  fun update(data: GCIssueComment) {
//    dataState.value = data
//  }
//
//  private inner class EditViewModel(initialText: String)
//    : CodeReviewSubmittableTextViewModelBase(project, cs, initialText), CodeReviewTextEditingViewModel {
//    override fun save() {
//      submit { text ->
//        val updated = commentsData.updateComment(EmptyProgressIndicator(), id, text).await()
//        dataState.update {
//          it.copy(body = updated)
//        }
//        stopEditing()
//      }
//    }
//
//    override fun stopEditing() = this@UpdateableGCPRTimelineCommentViewModel.stopEditing()
//
//    fun dispose() = cs.cancel()
//  }
//}
